﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Web;

namespace Sun.Mvc.Framework
{
    public static class StringExtions
    {
        /// <summary>
        /// Json序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ToJson<T>(this T t)
        {
            return JsonConvert.SerializeObject(t);
        }

        /// <summary>
        /// 获取结果信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToMessage(this int input)
        {
            if (input > 0)
            {
                XmlHelper helper = new XmlHelper(HttpContext.Current.Server.MapPath("/SystemConfig/System.xml"));
                return helper.GetMessage(ToInt(input));
            }

            return "";
        }

        /// <summary>
        /// 转换成整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int ToInt(this object input)
        {
            if (!Convert.IsDBNull(input) && !input.Equals(null))
            {
                return Convert.ToInt32(input);
            }

            return 0;
        }
    }
}
